package com.abc.controller;

import com.abc.entity.User;
import com.abc.entity.UserLoginDTO;
import com.abc.service.IUserService;
import com.abc.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(path = "/user", produces = "application/json;charset=utf-8")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;
    @RequestMapping(value = "/register")
    @ResponseBody
    public JsonResult<Void> register(User user){
        userService.register(user);
        JsonResult<Void> jr = new JsonResult<Void>();
        jr.setState(1);
        return jr;
    }
    @RequestMapping(path = "/login", method = RequestMethod.POST)
    public JsonResult<Void> login(UserLoginDTO loginDTO, HttpServletRequest request) {
        userService.login(loginDTO);
        request.getSession().setAttribute("user", userService.findByUsername(loginDTO.getUsername()));
        return new JsonResult(1);
    }
    @RequestMapping(path = "/delete", method = RequestMethod.POST)
    public JsonResult<Void> delete() {
        return new JsonResult(1,"删除成功");
    }

    @RequestMapping(path = "/update", method = RequestMethod.POST)
    public JsonResult<Void> update() {
        return new JsonResult(1,"修改成功");
    }
}
